package com.wz.jiangsu.main.td;

import com.taosdata.jdbc.TSDBPreparedStatement;
import org.springframework.util.StopWatch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;

public class ParameterBindingBasicDemo2 {

    // modify host to your own
    private static final String host = "10.25.10.250";
    //    private static final String host = "127.0.0.1";
    private static final Random random = new Random(System.currentTimeMillis());
    private static final int numOfSubTable = 1, numOfRow = 1;

    private static final String[] regions = {"east", "west", "north", "south", "center"};

    private static final String[] types = {"plc", "dcs", "ipc", "cnc", "fcs"};

    public static void main(String[] args) throws SQLException {

        String jdbcUrl = "jdbc:TAOS://" + host + ":6030/";
        Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata");

        init(conn);

        String sql = "INSERT INTO ? USING devices TAGS(?,?,?) VALUES (?,?,?,?,?,?)";

        try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {

            StopWatch sw = new StopWatch();


            sw.start("插入 1 条数据");

            for (int i = 1; i <= numOfSubTable; i++) {
                // set table name
                pstmt.setTableName("host" + i);

                // set tags
                pstmt.setTagString(0, "host" + i);

                // 生成随机数（0到4之间的整数）
                int randomNumber = random.nextInt(5);  // 生成的随机数范围是 [0, 4]

                // 根据随机数获取对应的区域名称
                String randomRegion = regions[randomNumber];

                pstmt.setTagString(1, randomRegion);

                int randomNumberType = random.nextInt(5);
                pstmt.setTagString(2, types[randomNumberType]);

                // set column ts
                ArrayList<Long> tsList = new ArrayList<>();
                long current = System.currentTimeMillis();
                for (int j = 0; j < numOfRow; j++)
                    tsList.add(current + j);
                pstmt.setTimestamp(0, tsList);

                // set column cpu
                ArrayList<Float> f1List = new ArrayList<>();
                for (int j = 0; j < numOfRow; j++)
                    f1List.add(random.nextFloat() * 30);
                pstmt.setFloat(1, f1List);

                // set column memory
                ArrayList<Float> f2List = new ArrayList<>();
                for (int j = 0; j < numOfRow; j++)
                    f2List.add(random.nextFloat() * 30);
                pstmt.setFloat(2, f2List);

                // set column disk
                ArrayList<Float> f3List = new ArrayList<>();
                for (int j = 0; j < numOfRow; j++)
                    f3List.add(random.nextFloat() * 30);
                pstmt.setFloat(3, f3List);

                // set column network
                ArrayList<Float> f4List = new ArrayList<>();
                for (int j = 0; j < numOfRow; j++)
                    f4List.add(random.nextFloat() * 30);
                pstmt.setFloat(4, f4List);

                // set column process
                ArrayList<Integer> f5List = new ArrayList<>();
                for (int j = 0; j < numOfRow; j++)
                    f5List.add(random.nextInt(300));
                pstmt.setInt(5, f5List);


                // add column
                pstmt.columnDataAddBatch();
            }
            // execute column
            pstmt.columnDataExecuteBatch();
            sw.stop();
            System.out.println("sw.prettyPrint() = " + sw.prettyPrint());

        }


        conn.close();
    }

    private static void init(Connection conn) throws SQLException {
        try (Statement stmt = conn.createStatement()) {
            stmt.execute("CREATE DATABASE IF NOT EXISTS test");
            stmt.execute("USE test");
            stmt.execute("CREATE STABLE IF NOT EXISTS devices (ts TIMESTAMP, cpu FLOAT, memory FLOAT, disk FLOAT, network FLOAT, processes INT) TAGS (host_ip BINARY(10), region BINARY(10), type BINARY(10))");
        }
    }
}
